package gov.pbc.nn.core.job;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import gov.pbc.nn.core.bdf.entity.Dept;
//import gov.pbc.nn.core.bdf.view.DeptInit;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;

import javax.annotation.Resource;

import org.apache.axis.AxisFault;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.hibernate.Session;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import gov.pbc.nn.core.job.SynchronousDataService;
import gov.pbc.nn.core.job.SynchronousDataServiceLocator;
import gov.pbc.nn.core.job.SynchronousDataSoapBindingStub;

import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
//@SuppressWarnings("deprecation")
@Component(JobUpdateDept.BEAN_ID)
public class JobUpdateDept extends CoreHibernateDao implements Job    {
	public static final String BEAN_ID = "gov.pbc.nn.core.job.JobUpdateDept";
			
	//private static String msg = "";
	
	
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		SAXReader reader = new SAXReader();
		// 通过read方法读取一个文件 转换成Document对象
		Document document = null;
		String msg = "";
		SynchronousDataService locator = new SynchronousDataServiceLocator();
		//SynchronousDataSoapBindingStub stub = (SynchronousDataSoapBindingStub)locator.getSynchronousData();
		java.net.URL url = null;
		try {
			url = new java.net.URL("http://11.72.25.118:8080/loginoa/services/SynchronousData.jws?wsdl");
			try {
				SynchronousDataSoapBindingStub stub = new SynchronousDataSoapBindingStub(url, locator);
				String syscode = "0001";
				msg = stub.getSynDeptDatas(syscode);
			} catch (AxisFault e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		
		
		String msg1 = msg.replace("encoding='UTF-8'?", "encoding='GBK'?");
		try {
			document = reader.read(new ByteArrayInputStream(msg1.getBytes()));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 获取根节点元素对象
		Element node = document.getRootElement();
		//String type = node.getName();

		final List<Element> list1 = new ArrayList<Element>();
		list1.addAll(node.elements());
		//int p = list1.size();
		if(list1.size() > 0){
			this.doInHibernateSession(new ISessionCallback<Void>() {

				@Override
				public Void doInSession(Session session) {
					
					//int p = list1.size();
					for (int i = 0; i < (list1.size() - 1); i++) {
						Dept dept = new Dept();
						String type = list1.get(i).attributeValue("operType");
						dept.setId(list1.get(i).element("ou").getText());
						dept.setName(list1.get(i).element("name").getText());
						String parentId = list1.get(i)
								.element("PBCSupervisorydepartment").getText();
						dept.setParentId(StringUtils.isEmpty(parentId) ? null
								: parentId);
//						dept.setCode(list1.get(i).element("PBCOrgNumber").getText());
//						dept.setType(Integer.parseInt(list1.get(i)
//								.element("PBCOuType").getText()));
						
						String[] deptTemp = dept.getName().split("/");
						if (deptTemp[0].contains("支行")){
							int deptLevel = deptTemp.length;
							dept.setDeptLevel(String.valueOf(deptLevel));
						}
						
						dept.setCompanyId("PBCNN");
						if(type.equals("add")){
							session.save(dept);
						}

						else if(type.equals("delete")){
							session.delete(dept);
						}
						else{
							session.update(dept);
						}
						session.flush();
						session.clear();
					}
					SynchronousDataService locator = new SynchronousDataServiceLocator();
					//SynchronousDataSoapBindingStub stub = (SynchronousDataSoapBindingStub)locator.getSynchronousData();
					java.net.URL url = null;
					String synendtime = list1.get(list1.size() - 1).getText();
					try {
						String syscode = "0001";
						url = new java.net.URL("http://11.72.25.118:8080/loginoa/services/SynchronousData.jws?wsdl");
						SynchronousDataSoapBindingStub stub = new SynchronousDataSoapBindingStub(url, locator);
						String result = stub.delSynDeptDatas(syscode, synendtime);
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (AxisFault e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					return null;
				}
			});
		}
		
	}

}
